package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo8CanalJson {
  def main(args: Array[String]): Unit = {

    //创建flink的环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //流模式
      .build()

    /**
      * 创建flink sql 环境
      *
      */

    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    bsTableEnv.executeSql(
      """
        |CREATE TABLE canal_student (
        | id STRING,
        | name STRING,
        | age BIGINT,
        | gender STRING,
        | clazz STRING
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'bigdata.student1',
        | 'properties.bootstrap.servers' = 'master:9092',
        | 'properties.group.id' = 'asdasssasdassd',
        | 'format' = 'canal-json' ,
        | 'scan.startup.mode' = 'earliest-offset',
        | 'canal-json.ignore-parse-errors' = 'true'
        |)
        |
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |CREATE TABLE clazz_num (
        |  clazz STRING,
        |  num BIGINT,
        |  PRIMARY KEY (clazz) NOT ENFORCED
        |) WITH (
        |   'connector' = 'jdbc',
        |   'url' = 'jdbc:mysql://master:3306/bigdata?useUnicode=true&characterEncoding=utf-8',
        |   'table-name' = 'clazz_num',
        |   'username'='root',
        |   'password'='123456'
        |)
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |insert into clazz_num
        |select clazz,count(1) as num from canal_student group by clazz
        |
      """.stripMargin)


  }

}
